package com.uh.calculation.util;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.Operators;
import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class OperatorXmlUtil {

    public  static List<OperatorVal>  readXml(String  operatorName) {
        List<OperatorVal>  firsts =null;
        try {
            String path = ResourceUtils.getURL("classpath:calculation/operator.xml").getPath();
            File file = new File(path);
            //SAXReader saxReader = new SAXReader();
            JAXBContext context = JAXBContext.newInstance(Operators.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Object object = unmarshaller.unmarshal(file);
            Operators reciever = (Operators) object;
            List<OperatorVal> operatorVal = reciever.getOperatorVal();
            //OperatorVal first = operatorVal.stream().filter(OperatorVal -> Objects.equals(OperatorVal.getName(),1)).findFirst().get();
            firsts = operatorVal.stream().filter(OperatorVal -> Objects.equals(OperatorVal.getName(), operatorName)).collect(Collectors.toList());
        } catch (Exception e) {

        }
        return  firsts;
    }
}